package com.incar.chuxing.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.incar.chuxing.R;
import com.incar.chuxing.adapter.RechargeRecordAdapter;
import com.incar.chuxing.api.ApiStore;
import com.incar.chuxing.api.UrlConstant;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.bean.ChargeDetailBean;
import com.incar.chuxing.utils.MyApplication;
import com.incar.chuxing.utils.StatusBarUtil;
import com.incar.chuxing.utils.Toast.ToastCompat;
import com.jcodecraeer.xrecyclerview.XRecyclerView;


import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RechargeRecordActivity extends BaseActivity {
    @BindView(R.id.xr_charge_detail)
    XRecyclerView xr_charge_detail;
    @BindView(R.id.charge_detail_no_view)
    RelativeLayout charge_detail_no_view;
    private Context mContext = this;
    private int curPage = 1;
    private RechargeRecordAdapter adapter;
    @BindView(R.id.title_name)
    TextView titleName;
    @BindView(R.id.title_back)
    ImageView title_back;

    @Override
    public int getLayoutId() {
        return R.layout.activity_charge_detail;
    }

    @Override
    public void initView() {
        titleName.setText("充值明细");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xr_charge_detail.setLayoutManager(linearLayoutManager);
        xr_charge_detail.setItemAnimator(new DefaultItemAnimator());
        xr_charge_detail.setEmptyView(charge_detail_no_view);
        adapter = new RechargeRecordAdapter(mContext, R.layout.item_charge_detail);
        xr_charge_detail.setPullRefreshEnabled(true);
        xr_charge_detail.setLoadingMoreEnabled(true);
        xr_charge_detail.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                curPage = 1;
                loadNetworkData();
            }

            @Override
            public void onLoadMore() {
                curPage++;
                loadNetworkData();
            }
        });
        xr_charge_detail.setAdapter(adapter);
        title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void loadNetworkData() {
        if (((MyApplication) getApplication()).getUid() == null) {
            ToastCompat.makeText(mContext, getResources().getString(R.string.account_expire), Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(mContext, LoginActivity.class);
            startActivity(intent);
            return;
        }
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getChargeDetail(((MyApplication) getApplication()).getRequestFieldMap(),curPage + "", "10").enqueue(new Callback<ChargeDetailBean>() {
            @Override
            public void onResponse(Call<ChargeDetailBean> call, Response<ChargeDetailBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        ChargeDetailBean.AttachmentBean result = response.body().getAttachment();
                        if (response.body().getAttachment().getRechargeList().size() != 0) {
                            if (curPage == 1) {
                                adapter.clear();
                                adapter.replaceAll(result.getRechargeList());
                                xr_charge_detail.refreshComplete();
                            } else {
                                adapter.addAll(result.getRechargeList());
                                xr_charge_detail.loadMoreComplete();
                            }
                        } else {
                            if (curPage == 1) {
                                //charge_detail_no_view.setVisibility(View.VISIBLE);
                            }
                            //   xr_charge_detail.setNoMore(true);
                            xr_charge_detail.refreshComplete();
                            xr_charge_detail.loadMoreComplete();
                        }
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ChargeDetailBean> call, Throwable t) {
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        curPage = 1;
        loadNetworkData();
    }
}

